package com.cvit.controller;


import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.model.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cvit.model.Student;
import com.cvit.service.StudentService;
import com.cvit.utils.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class StudentController {

	@Autowired
	StudentService studentService;
	
	/**
	 * 查询所有
	 * */
	@RequestMapping("/getStudent")
	@ResponseBody
	public Msg getAchievements(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn, 10);
		List<Student> student = studentService.getAll();
		PageInfo<Student> page = new PageInfo<Student>(student, 10);
		return Msg.success().add("pageInfo", page);
	}
	
	/**
	 * 根据Id查询
	 * */
	@RequestMapping("/getStudentById")
	@ResponseBody
	public Msg getStudentById(@RequestParam(value = "Id") String Id){
		Student student = studentService.getStudentById(Id);
		return Msg.success().add("student", student);
	}
	
	/**
	 * 根据考生号、姓名、身份证号查询
	 * */
	@RequestMapping("/getStudentBys")
	@ResponseBody
	public Msg getStudentBys(Student student){
		Student stu = new Student();
//		List<Student> list1 = studentService.selectByExamineeNumber(student);
//		if(list1.size()==0){
//			return Msg.fail().add("student", "考生号错误");
//		}
		List<Student> list2 = studentService.selectByCardNumber(student);
		if(list2.size()==0){
			return Msg.fail().add("student", "身份证号错误");
		}
		List<Student> studentL = studentService.getStudentBys(student);
		return Msg.success().add("student", studentL);
	}
	
	/**
	 * 根据姓名、身份证号查询
	 * */
	@RequestMapping("/getStudentOne")
	@ResponseBody
	public Msg getStudentOne(Student student){
//		Student stu = new Student();
//		List<Student> list1 = studentService.selectByExamineeNumber(student);
//		if(list1.size()==0){
//			return Msg.fail().add("student", "考生号错误");
//		}
		List<Student> list2 = studentService.selectByCardNumber(student);
		if(list2.size()==0){
			return Msg.fail().add("student", "身份证号错误");
		}
		List<Student> students = studentService.getOne(student);
		System.out.println(student);
		return Msg.success().add("student", students);
	}
	
	/**
	 * 微信登录
	 * */
	@RequestMapping("/getLogin")
	@ResponseBody
	public Msg stuLogin(Student student){
		Student stu = new Student();
		List<Student> list2 = studentService.selectByCardNumber(student);
		if(list2.size()==0){
			return Msg.fail().add("student", "身份证号错误");
		}
		List<Student> studentL = studentService.stuLogin(student);
		return Msg.success().add("student", studentL);
	}
	
	/**
	 * 修改缴费状态
	 * */
	@RequestMapping("/studentUpdatepayType")
	@ResponseBody
	public Msg updatepayType(@RequestParam(value = "id")String id){
		studentService.updatePayType(id);
		return Msg.success();
	}
	
	
	/**
	 * 导入Excel
	 * */
	@RequestMapping("/StudentImport")
	@ResponseBody
	public Msg impotr(HttpServletRequest request, Student student) throws Exception {
	     //获取上传的文件 
	     MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
	     MultipartFile file = multipart.getFile("upfile");
	     InputStream in = file.getInputStream();
	     //数据导入  
	     studentService.importExcelInfo(in,file);
	     in.close();
	     //return Msg.success();
	     return null;
	}
	
	
}
